package com.ruoyi.wms.service.impl;

import java.util.List;

import com.ruoyi.wms.domain.WmsMaterialStock;
import com.ruoyi.wms.mapper.WmsMaterialStockMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.wms.mapper.WmsMaterialIncomeMapper;
import com.ruoyi.wms.domain.WmsMaterialIncome;
import com.ruoyi.wms.service.IWmsMaterialIncomeService;
import com.ruoyi.common.core.text.Convert;

/**
 * 物料入库Service业务层处理
 * 
 * @author chao
 * @date 2024-07-23
 */
@Service
public class WmsMaterialIncomeServiceImpl implements IWmsMaterialIncomeService 
{
    @Autowired
    private WmsMaterialIncomeMapper wmsMaterialIncomeMapper;

    @Autowired
    private WmsMaterialStockMapper wmsMaterialStockMapper;

    /**
     * 查询物料入库
     * 
     * @param id 物料入库主键
     * @return 物料入库
     */
    @Override
    public WmsMaterialIncome selectWmsMaterialIncomeById(Long id)
    {
        return wmsMaterialIncomeMapper.selectWmsMaterialIncomeById(id);
    }

    /**
     * 查询物料入库列表
     * 
     * @param wmsMaterialIncome 物料入库
     * @return 物料入库
     */
    @Override
    public List<WmsMaterialIncome> selectWmsMaterialIncomeList(WmsMaterialIncome wmsMaterialIncome)
    {
        return wmsMaterialIncomeMapper.selectWmsMaterialIncomeList(wmsMaterialIncome);
    }

    /**
     * 新增物料入库
     * 
     * @param wmsMaterialIncome 物料入库
     * @return 结果
     */
    @Override
    public int insertWmsMaterialIncome(WmsMaterialIncome wmsMaterialIncome)
    {
        return wmsMaterialIncomeMapper.insertWmsMaterialIncome(wmsMaterialIncome);
    }

    /**
     * 修改物料入库
     * 
     * @param wmsMaterialIncome 物料入库
     * @return 结果
     */
    @Override
    public int updateWmsMaterialIncome(WmsMaterialIncome wmsMaterialIncome)
    {
        return wmsMaterialIncomeMapper.updateWmsMaterialIncome(wmsMaterialIncome);
    }

    /**
     * 批量删除物料入库
     * 
     * @param ids 需要删除的物料入库主键
     * @return 结果
     */
    @Override
    public int deleteWmsMaterialIncomeByIds(String ids)
    {
        return wmsMaterialIncomeMapper.deleteWmsMaterialIncomeByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除物料入库信息
     * 
     * @param id 物料入库主键
     * @return 结果
     */
    @Override
    public int deleteWmsMaterialIncomeById(Long id)
    {
        return wmsMaterialIncomeMapper.deleteWmsMaterialIncomeById(id);
    }

    @Override
    public void executeStorage(Long Rowid, Long MaterialId, Double num) {

        WmsMaterialStock wmsMaterialStock = wmsMaterialStockMapper.selectWmsMaterialStockByMaterialId(MaterialId);
        if(wmsMaterialStock == null){

            WmsMaterialIncome wmsMaterialIncome = wmsMaterialIncomeMapper.selectWmsMaterialIncomeById(Rowid);
            Long storgeId = wmsMaterialIncome.getId();
            Long storgeMaterialId = wmsMaterialIncome.getMaterialId();
            String storgeName = wmsMaterialIncome.getName();
            String storgeModel = wmsMaterialIncome.getModel();
            String storgeUnitName = wmsMaterialIncome.getUnitName();
            Double storgeNum = wmsMaterialIncome.getNum();
            String common = wmsMaterialIncome.getCommon();


            WmsMaterialStock StorgeTable = new WmsMaterialStock();
            StorgeTable.setId(storgeId);
            StorgeTable.setMaterialId(storgeMaterialId);
            StorgeTable.setName(storgeName);
            StorgeTable.setModel(storgeModel);
            StorgeTable.setUnitName(storgeUnitName);
            StorgeTable.setStockNum(storgeNum);
            StorgeTable.setCommon(common);

            wmsMaterialStockMapper.insertWmsMaterialStock(StorgeTable);

        }else{
            wmsMaterialStockMapper.executeStorage(MaterialId,num);
        }
        wmsMaterialIncomeMapper.updateWmsMaterialIncomeStatus_AllreadyIncome_ById(Rowid);
    }
}
